package org.sith.taskrunner.ejb.services.impl;

import org.sith.taskrunner.api.exceptions.TransformInputDataException;
import org.sith.taskrunner.api.user.Role;
import org.sith.taskrunner.api.user.User;
import org.sith.taskrunner.ejb.db.entity.UserEntity;
import org.sith.taskrunner.ejb.services.UserManagerService;
import org.sith.taskrunner.ejb.transformers.UserInputData;
import org.sith.taskrunner.ejb.transformers.UserXMLTransformer;
import org.sith.taskrunner.util.UserImpl;

import javax.ejb.Stateful;
import java.io.InputStream;
import java.io.Serializable;
import java.util.*;

//@Stateful(name = "UserServiceEJB")
public class UserManagerServiceEJB implements Serializable, UserManagerService {
    private Map<String, User> userMap;

    public UserManagerServiceEJB() {
        try {
            InputStream inputStream = this.getClass().getClassLoader()
                    .getResourceAsStream("Users.xml");
            UserXMLTransformer userParser = new UserXMLTransformer();
            UserInputData input = userParser.transform(inputStream);
            userMap = input.getUserMap();
        } catch (TransformInputDataException e) {
            throw new IllegalStateException(e);
        }
    }


    @Override
    public User getUserByNameAndPassword(String usrName, String usrPass) {
        User user = userMap.get(usrName);
        if (user == null) {
            return null;
        }
        if (user.getLogin().equals(usrName)
                && (user.getPassword().equals(usrPass))) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public List<String> getAllUserWithUserRole() {
        List<String> users = getUserByRole(Role.USER);
        return users;
    }

    @Override
    public List<String> getAllUserWithAdminRole() {
        List<String> users = getUserByRole(Role.ADMIN);
        return users;
    }

    @Override
    public List<String> getAllUserWithTaskOwnerRole() {
        List<String> users = getUserByRole(Role.TASK_OWNER);
        return users;
    }

    private List<String> getUserByRole(Role role) {
        List<String> users = new ArrayList<String>();
        for (Map.Entry<String, User> entry : userMap.entrySet()) {
            User user = entry.getValue();
            if ((user != null) && (user.getRole() == role)) {
                users.add(entry.getKey());
            }
        }
        return users;
    }

    @Override
    public List<UserImpl> getAllUsers() {
        return null;
    }

    @Override
    public Map<String, Role> getUsersAndRoles() {
        Map<String, Role> usersRole = new TreeMap<String, Role>();
        for (Map.Entry<String, User> entry : userMap.entrySet()) {
            if (entry.getValue() != null) {
                usersRole.put(entry.getValue().getLogin(), entry.getValue()
                        .getRole());
            }
        }
        return usersRole;
    }


    @Override
    public int getUsersCount() {
        return userMap.size();
    }


	@Override
	public void addUser(UserEntity user) {
		throw new IllegalStateException();
	}


	@Override
	public void removeUserById(Long id) {
		throw new IllegalStateException("Unimployment method removeUserById form userServiceEJB");
		
	}

    @Override
    public boolean updateActiveState(Long id, boolean active) {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    public boolean updateRole(Long id, Role roleEntity) {
        throw new IllegalStateException("Not implemented yet");
    }

}
